extends Control

## 最大生命值, 来自玩家的state节点
@onready var maxHp = $"../../Player/State".maxHp

## 当前生命值UI
@onready var nowHpUI = $NowHP

## 护甲
@onready var armorUI = $Armor

var nowHp = null
@onready var armor = $"../../Player/State".armor

'''
	初始化
'''
func _ready():
	# 最大生命值UI
	var maxHpUI = $MaxHP
	nowHp = maxHp
	maxHpUI.size.x = maxHp * 15
	nowHpUI.size.x = maxHp * 15
	nowHpUI.size.y = 11
	armorUI.size.x = armor * 11
	armorUI.size.y = 15
	
func subX(x):
	if x < 0: return
	elif self.nowHp - x <= 0: x = self.nowHp
	
	# 先检测是否还有护甲
	if self.armor > 0:
		if self.armor >= x:
			self.armor -= x
			x = 0
		else:
			x -= self.armor
			self.armor = 0
		
	self.nowHp -= x
	self.nowHpUI.size.x = self.nowHp * 15
	self.armorUI.size.x = self.armor * 11
	
	
func addX(x):
	if x < 0: return
	elif nowHp + x >= maxHp: x = maxHp - nowHp
	
	nowHp += x
	nowHpUI.size.x = nowHp * 15
	
	
func addArmorX(x):
	self.armor += x
	self.armorUI.size.x = self.armor * 11
	

func getNowHp():
	return self.nowHp

